package appapi.service.impl;

import appapi.Global.BusinessException;
import appapi.Request.record.ScenePageReq;
import appapi.Request.record.SceneReq;
import appapi.Request.record.SectReq;
import appapi.Request.record.SectionPageReq;
import appapi.Vo.ScenePageVo;
import appapi.Vo.brider.ScetionNameAndIdVo;
import appapi.Vo.brider.SectionDetailVo;
import appapi.Vo.brider.SectionPageVo;
import appapi.Vo.brider.SectionVo;
import appapi.dao.AnnexMapper;
import appapi.dao.SectionMapper;
import appapi.dto.AnnexType;
import appapi.entity.Zhongjiao.AnnexDto;
import appapi.entity.Zhongjiao.SectionDto;
import appapi.entity.app.AnnexEntity;
import appapi.entity.basic.UserInfo;
import appapi.entity.bridge.SectionEntity;
import appapi.service.SectionService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author dhq
 * @version 1.0
 * @date 2023/9/8  9:58
 */
@Service
public class SectionServiceImpl extends ServiceImpl<SectionMapper, SectionEntity> implements SectionService {

    @Resource
    AnnexMapper annexMapper;

    @Autowired
    SectionMapper sectionMapper;


    @Override
    public PageInfo<SectionPageVo> getSectionListByPage(SectionPageReq req)
    {
        String json = JSON.toJSONString(req);
        SectReq dataReq = JSON.parseObject(json, new TypeReference<SectReq>() {
        });
        PageInfo<SectionPageVo> pageInfo = PageHelper.startPage(req.getCurrPage(), req.getPageSize())
                .doSelectPageInfo(() -> sectionMapper.getSectionList(dataReq));
        return pageInfo;
    }

    /**
     * 工段列表
     * @param req
     * @return
     */
    @Override
    public List<SectionPageVo> getSecList(SectReq req) {
        return sectionMapper.getSectionList(req);
    }

    @Override
    public List<ScetionNameAndIdVo> getSecListByProjId(Long projId) {
        return sectionMapper.getSecNameAndId(projId);
    }


    /**
     * 添加工段信息
     * @param dto
     */
    @Override
    public void InsertSection(UserInfo userInfo, SectionDto dto) {
        SectionEntity entity = dto.Entity();
        entity.setCreateTime(LocalDateTime.now());
        entity.setLastModifyTime(LocalDateTime.now());
        entity.setSectStatus(0);
        entity.setCreateUser(userInfo.getUserid());
        entity.setLastModifyUser(userInfo.getUserid());
        entity.setSectDuration(LocalDateTime.now());
        if(sectionMapper.selectSectNoCount(dto.getProjId(),0L,dto.getSectNo())>0)
        {
            throw new BusinessException(-1,"编号已存在不能重复添加");
        }
        if(sectionMapper.selectSectNameCount(dto.getProjId(),0L,dto.getSectName())>0)
        {
            throw new BusinessException(-1,"名称已存在不能重复添加");
        }
        boolean save = this.save(entity);
        if(!save){
            throw new BusinessException(500,"插入工段信息失败");
        }
        //有附件则新增附件
        if (dto.getAnnexUrl().size() > 0) {
            //暂搁置
            List<AnnexEntity> annexCollect = dto.getAnnexUrl().stream().map(annex -> {
                AnnexEntity annexEntity = new AnnexEntity();
                annexEntity.setCreateTime(LocalDateTime.now());
                annexEntity.setUpdateTime(LocalDateTime.now());
                annexEntity.setAnnexTitle("工段");
                annexEntity.setRecId(entity.getSectId());
                annexEntity.setAnnexType(AnnexType.AnnexTypeEnum.section.name());
                annexEntity.setAnnexUrl(annex);
                annexEntity.setCreateUser(userInfo.getUsername());
                return annexEntity;
            }).collect(Collectors.toList());
            annexMapper.insertList(annexCollect);
        }

    }

    /**
     * 删除工段管理
     * @param secId
     */
    @Override
    public Boolean deleteSection(Long secId) {
        //删除附件
        annexMapper.deleteByRecId(AnnexType.AnnexTypeEnum.section.name(),Collections.singletonList(secId));
        return this.removeById(secId);
    }


    /**
     * 查询工段管理详情
     * @param secId
     */
    @Override
    public SectionVo getDetail(Long secId) {
        SectionVo dto = new SectionVo();
        SectionDetailVo sectionDetail = sectionMapper.getSectionDetail(secId);
        dto.setDetail(sectionDetail);
        List<AnnexDto> annexDtos = annexMapper.selectAllByRecIds(AnnexType.AnnexTypeEnum.section.name(), secId);
        dto.setAnnexUrl(annexDtos.stream().map(AnnexDto::getAnnexUrl).collect(Collectors.toList()));
        return dto;
    }


    /**
     * 更新工段信息
     * @param dto
     */
    @Override
    public void UpdateSection(SectionDto dto) {
        SectionEntity entity = dto.Entity();
        entity.setLastModifyTime(LocalDateTime.now());
        if(sectionMapper.selectSectNoCount(dto.getProjId(),dto.getSectId(),dto.getSectNo())>0)
        {
            throw new BusinessException(-1,"编号已使用");
        }
        if(sectionMapper.selectSectNameCount(dto.getProjId(),dto.getSectId(),dto.getSectName())>0)
        {
            throw new BusinessException(-1,"名称已使用");
        }
        Integer iRet = sectionMapper.updateByPrimaryKeySelective(entity);
        if(iRet<=0){
         throw new BusinessException(-1,"更新工段信息失败") ;
        }
        //附件修改
        annexMapper.deleteByRecId(AnnexType.AnnexTypeEnum.section.name(), Collections.singletonList(dto.getSectId()));
        //有附件则新增附件
        if (dto.getAnnexUrl().size() > 0) {
            //暂搁置
            //String[] user = JwtUtil.getUserInfoByJwtToken(request);
            List<AnnexEntity> annexCollect = dto.getAnnexUrl().stream().map(annex -> {
                AnnexEntity annexEntity = new AnnexEntity();
                annexEntity.setCreateTime(LocalDateTime.now());
                annexEntity.setUpdateTime(LocalDateTime.now());
                annexEntity.setAnnexTitle("工段");
                annexEntity.setRecId(dto.getSectId());
                annexEntity.setAnnexType(AnnexType.AnnexTypeEnum.selfCheck.name());
                annexEntity.setAnnexUrl(annex);
                annexEntity.setCreateUser(dto.getCreateUser().toString());
                return annexEntity;
            }).collect(Collectors.toList());
            annexMapper.insertList(annexCollect);
        }

    }




}
